package com.yy.wga.resource.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.wga.common.dto.PageBean;
import com.yy.wga.common.interceptor.RequestUserHolder;
import com.yy.wga.common.utils.common.StringUtils;
import com.yy.wga.common.utils.mapping.BeansUtils;
import com.yy.wga.module.admin.request.AdminResourceConfigPageRequest;
import com.yy.wga.module.admin.response.AdminResourceConfigPageResponse;
import com.yy.wga.module.resource.entity.pojo.ResourceConfigDO;
import com.yy.wga.module.resource.mapper.ResourceConfigMapper;
import com.yy.wga.module.resource.request.GetResourceRequest;
import com.yy.wga.module.resource.request.ListResourceRequest;
import com.yy.wga.module.resource.response.GetResourceResponse;
import com.yy.wga.module.resource.response.ListResourceResponse;
import com.yy.wga.module.resource.service.ResourceConfigService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 资源位配置(ResourceConfig)表服务实现类
 *
 * @author guochao
 * @since 2024-10-31 22:48:51
 */
@Service
public class ResourceConfigServiceImpl extends ServiceImpl<ResourceConfigMapper, ResourceConfigDO> implements ResourceConfigService {
    @Override
    public PageBean<AdminResourceConfigPageResponse> pageData(AdminResourceConfigPageRequest request) {
        LambdaQueryWrapper<ResourceConfigDO> queryWrapper = new LambdaQueryWrapper<ResourceConfigDO>()
                .eq(ResourceConfigDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .like(StringUtils.isNotBlank(request.getTitle()), ResourceConfigDO::getTitle, request.getTitle())
                .eq(request.getType() != null, ResourceConfigDO::getType, request.getType())
                .eq(request.getRegion() != null, ResourceConfigDO::getRegion, request.getRegion());
        if (request.getStatus() != null) {
            // 状态  0：未开始 1：已上架 2：已下架
            if (request.getStatus() == 0) {
                queryWrapper.ne(ResourceConfigDO::getStatus, 2)
                        .ge(ResourceConfigDO::getStartTime, new Date());
            } else if (request.getStatus() == 1) {
                Date now = new Date();
                queryWrapper.ne(ResourceConfigDO::getStatus, 2)
                        .le(ResourceConfigDO::getStartTime, now)
                        .ge(ResourceConfigDO::getEndTime, now);
            } else {
                // status = 2 or endTime < now
                queryWrapper.and(wrapper -> wrapper.eq(ResourceConfigDO::getStatus, 2)
                                .or(query -> query.lt(ResourceConfigDO::getEndTime, new Date())));
            }
        }
        List<ResourceConfigDO> configs = baseMapper.selectList(queryWrapper);
        Date now = new Date();
        for (ResourceConfigDO config : configs) {
            if (config.getStatus() == 2) {
                continue;
            }
            if (now.before(config.getStartTime())) {
                config.setStatus(0);
            } else if (now.after(config.getEndTime())) {
                config.setStatus(2);
            } else {
                config.setStatus(1);
            }
        }
        PageBean<AdminResourceConfigPageResponse> pageBean = new PageBean<>();
        pageBean.setPageInfo(configs);
        pageBean.setList(BeansUtils.mapList(configs, AdminResourceConfigPageResponse.class));
        return pageBean;
    }

    @Override
    public List<GetResourceResponse> get(GetResourceRequest request) {
        Date now = new Date();
        List<ResourceConfigDO> resourceConfigList = baseMapper.selectList(new LambdaQueryWrapper<ResourceConfigDO>()
                .eq(ResourceConfigDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .lt(ResourceConfigDO::getStartTime, now)
                .gt(ResourceConfigDO::getEndTime, now)
                .in(ResourceConfigDO::getStatus, Arrays.asList(0, 1))
                .eq(ResourceConfigDO::getType, request.getType())
                .in(ResourceConfigDO::getRegion, request.getRegion()));
        Map<Integer, List<ResourceConfigDO>> resourceConfigMap = resourceConfigList.stream()
                .collect(Collectors.groupingBy(ResourceConfigDO::getType));

        List<GetResourceResponse> responseList = new ArrayList<>(resourceConfigMap.size());
        for (Map.Entry<Integer, List<ResourceConfigDO>> entry : resourceConfigMap.entrySet()) {
            Integer type = entry.getKey();
            List<ResourceConfigDO> resourceConfigs = entry.getValue();

            GetResourceResponse response = new GetResourceResponse();
            response.setType(type);
            List<GetResourceResponse.Config> configs = BeansUtils.mapList(resourceConfigs, GetResourceResponse.Config.class);
            response.setConfigs(configs);
            responseList.add(response);
        }
        return responseList;
    }

    @Override
    public List<ListResourceResponse> listData(ListResourceRequest request) {
        Date now = new Date();
        List<ResourceConfigDO> resourceConfigList = baseMapper.selectList(new LambdaQueryWrapper<ResourceConfigDO>()
                .eq(ResourceConfigDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .lt(ResourceConfigDO::getStartTime, now)
                .gt(ResourceConfigDO::getEndTime, now)
                .in(ResourceConfigDO::getStatus, Arrays.asList(0, 1))
                .in(!CollectionUtils.isEmpty(request.getType()), ResourceConfigDO::getType, request.getType())
                .eq(ResourceConfigDO::getRegion, request.getRegion()));
        return BeansUtils.mapList(resourceConfigList, ListResourceResponse.class);
    }
}
